#include "VIN_SoundManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "../Core/CGame.h"

VIN_SoundManager::VIN_SoundManager()
{
	Death1 = Death2 = Punch1 = Punch2 = Punch3 = -1;
	Woosh1 = Woosh2 = Woosh3 = Hurt1 = Hurt2 = Hurt3 = Hurt4 = -1;
	Gunshot1 = Gunshot2 = Gunshot3 = MenuSelect = MenuConfirm = -1;
	Pickup = Explosion = Alarm = Start = placerock = castleup = -1;
	CityID = TitleBGM = MenuBGM = CreditBGM = Highscores = -1;

}
VIN_SoundManager::~VIN_SoundManager()
{

}
VIN_SoundManager::VIN_SoundManager(const VIN_SoundManager& copy)
{

}
//VIN_SoundManager::VIN_SoundManager &operator=(const VIN_SoundManager& assign)
//{
//
//	return this;
//}

VIN_SoundManager* VIN_SoundManager::GetInstance()
{
	static VIN_SoundManager instance;
	return& instance;
}


bool VIN_SoundManager::Startup(void)
{
	fmod = CSGD_FModManager::GetInstance();
	CityID = fmod->LoadSound("resources/sounds/GameMusic.mp3", FMOD_LOOP_NORMAL);
	Highscores = fmod->LoadSound("resources/sounds/highscores.mp3", FMOD_LOOP_NORMAL);
	CreditBGM = fmod->LoadSound("resources/sounds/CreditMusic.mp3", FMOD_LOOP_NORMAL);
	TitleBGM = fmod->LoadSound("resources/sounds/MenuMusic.mp3", FMOD_LOOP_NORMAL);
	win = fmod->LoadSound("resources/sounds/win.mp3");
	castleup = fmod->LoadSound("resources/sounds/castleup.mp3");
	gameover = fmod->LoadSound("resources/sounds/gameover.mp3");
	//HighScoreBGM = fmod->LoadSound("resources/sounds/TitleMenu.mp3", FMOD_LOOP_NORMAL);
	//MenuBGM = fmod->LoadSound("resources/sounds/othermenus.mp3", FMOD_LOOP_NORMAL);
	Punch1 = fmod->LoadSound("resources/sounds/Swordhit1.wav");
	Punch2 = fmod->LoadSound("resources/sounds/Swordhit2.wav");
	Punch3 = fmod->LoadSound("resources/sounds/Swordhit3.wav");
	Punch4 = fmod->LoadSound("resources/sounds/Swordhit4.wav");
	Level1 = fmod->LoadSound("resources/sounds/Level1.wav");
	Level2 = fmod->LoadSound("resources/sounds/Level2.wav");
	placerock = fmod->LoadSound("resources/sounds/rockplace.wav");
	Gunshot1 = fmod->LoadSound("resources/sounds/shotsound1.wav");
	Gunshot2 = fmod->LoadSound("resources/sounds/shotsound2.wav");
	//Death1 = fmod->LoadSound("resources/sounds/Death1.wav");
	//Death2 = fmod->LoadSound("resources/sounds/Death2.wav");
	Explosion = fmod->LoadSound("resources/sounds/explosion.wav");
	MenuConfirm = fmod->LoadSound("resources/sounds/menuconfirm.wav");
	MenuSelect = fmod->LoadSound("resources/sounds/menuselect.wav");
	Pickup = fmod->LoadSound("resources/sounds/CoinPickup.wav");
	Woosh1 = fmod->LoadSound("resources/sounds/poison1.wav");
	Woosh2 = fmod->LoadSound("resources/sounds/poison2.wav");
	Woosh3 = fmod->LoadSound("resources/sounds/poison3.wav");
	Hurt1 = fmod->LoadSound("resources/sounds/hurt1.wav");
	Hurt2 = fmod->LoadSound("resources/sounds/hurt2.wav");
	Hurt3 = fmod->LoadSound("resources/sounds/hurt3.wav");
	Hurt4 = fmod->LoadSound("resources/sounds/hurt4.wav");
	Start = fmod->LoadSound("resources/sounds/Start.wav");
	//Alarm = fmod->LoadSound("resources/sounds/Alarm.wav", FMOD_LOOP_NORMAL);

	while(CityID == -1)
	{

	}

	return true;
}
void VIN_SoundManager::Shutdown(void)
{
	fmod = CSGD_FModManager::GetInstance();
	fmod->ShutdownFModManager();
}

void VIN_SoundManager::PlayBGM(int Screen)
{
	switch(Screen)
	{
	case 1:
		StopSound(Highscores);
		StopSound(CreditBGM);
		StopSound(CityID);
		StopSound(gameover);
		StopGameSounds();
		PlaySound(TitleBGM);
		break;
	case 2:
		StopAllSounds();
		PlaySound(CreditBGM);
		break;
	case 3:
		PlaySound(MenuBGM);
		break;
	case 4:
		StopAllSounds();
		PlaySound(CityID);
		break;
	case 5:
		StopAllSounds();
		PlaySound(Highscores);
		break;

	}
}
void VIN_SoundManager::PlaySound(int SoundID)
{
	if(SoundID != -1)
	{
		//if(!fmod->IsSoundPlaying(SoundID))
		fmod->PlaySound(SoundID);
	}
}
void VIN_SoundManager::PlayCastleup()
{
	PlaySound(castleup);
}

void VIN_SoundManager::PlayLevelUp()
{
	PlaySound(Level1);
	PlaySound(Level2);
}

void VIN_SoundManager::PlayWin()
{
	StopAllSounds();
	if(!fmod->IsSoundPlaying(win))
		PlaySound(win);
}
void VIN_SoundManager::PlayGameOver()
{
	StopAllSounds();
	if(!fmod->IsSoundPlaying(gameover))
		PlaySound(gameover);
}

void VIN_SoundManager::PlayHurt()
{
	int randhurt = rand() % 4 + 1;
	switch(randhurt)
	{
	case 1:
		if(!fmod->IsSoundPlaying(Hurt1) && !fmod->IsSoundPlaying(Hurt2)
			&& !fmod->IsSoundPlaying(Hurt3)&& !fmod->IsSoundPlaying(Hurt4))
			PlaySound(Hurt1);
		break;
	case 2:
		if(!fmod->IsSoundPlaying(Hurt1) && !fmod->IsSoundPlaying(Hurt2)
			&& !fmod->IsSoundPlaying(Hurt3)&& !fmod->IsSoundPlaying(Hurt4))
			PlaySound(Hurt2);
		break;
	case 3:
		if(!fmod->IsSoundPlaying(Hurt1) && !fmod->IsSoundPlaying(Hurt2)
			&& !fmod->IsSoundPlaying(Hurt3)&& !fmod->IsSoundPlaying(Hurt4))
			PlaySound(Hurt3);
		break;
		case 4:
		if(!fmod->IsSoundPlaying(Hurt1) && !fmod->IsSoundPlaying(Hurt2)
			&& !fmod->IsSoundPlaying(Hurt3)&& !fmod->IsSoundPlaying(Hurt4))
			PlaySound(Hurt4);
	
		
	}
	
}

void VIN_SoundManager::PlayShot()
{
	if(!fmod->IsSoundPlaying(Gunshot1) && !fmod->IsSoundPlaying(Gunshot2))
		PlaySound(Gunshot2);
//else
		//PlaySound(Gunshot2);
}
void VIN_SoundManager::PlayRock()
{
	PlaySound(placerock);
}
void VIN_SoundManager::PlayPunch()
{
	fmod = CSGD_FModManager::GetInstance();

	//int randpunch = RandInt(1, 3);
	int randpunch = rand() % 4 + 1;
	switch(randpunch)
	{
	case 1:
		if(!fmod->IsSoundPlaying(Punch1) && !fmod->IsSoundPlaying(Punch2)
			&& !fmod->IsSoundPlaying(Punch3)&& !fmod->IsSoundPlaying(Punch4))
			PlaySound(Punch1);
		break;
	case 2:
		if(!fmod->IsSoundPlaying(Punch1) && !fmod->IsSoundPlaying(Punch2)
			&& !fmod->IsSoundPlaying(Punch3)&& !fmod->IsSoundPlaying(Punch4))
			PlaySound(Punch2);
		break;
	case 3:
		if(!fmod->IsSoundPlaying(Punch1) && !fmod->IsSoundPlaying(Punch2)
			&& !fmod->IsSoundPlaying(Punch3)&& !fmod->IsSoundPlaying(Punch4))
			PlaySound(Punch3);
		break;
		case 4:
		if(!fmod->IsSoundPlaying(Punch1) && !fmod->IsSoundPlaying(Punch2)
			&& !fmod->IsSoundPlaying(Punch3)&& !fmod->IsSoundPlaying(Punch4))
			PlaySound(Punch4);
	}
}

void VIN_SoundManager::PlayWoosh()
{
		if(!fmod->IsSoundPlaying(Woosh1))
			PlaySound(Woosh1);
		
		if(!fmod->IsSoundPlaying(Woosh1))
		{
			if(!fmod->IsSoundPlaying(Woosh2))
				PlaySound(Woosh2);	
		}
		
		if(!fmod->IsSoundPlaying(Woosh2))
		{
			if(!fmod->IsSoundPlaying(Woosh3))
				PlaySound(Woosh3);	
		}

	
}
void VIN_SoundManager::PlayDeath()
{
	int randdeath = rand() % 2+1;
	switch(randdeath)
	{
	case 1:
		PlaySound(Death1);
		break;
	case 2:
		PlaySound(Death2);
		break;
	}


}
void VIN_SoundManager::PlayExplosion()
{
	if(!fmod->IsSoundPlaying(Explosion))
		PlaySound(Explosion);
}
void VIN_SoundManager::PlayMenuSelect()
{
	//if(!fmod->IsSoundPlaying(MenuSelect))
	PlaySound(MenuSelect);
}
void VIN_SoundManager::PlayMenuConfirm()
{
	if(!fmod->IsSoundPlaying(MenuConfirm))
    PlaySound(MenuConfirm);
}
void VIN_SoundManager::PlayAlarm()
{
	//if(!fmod->IsSoundPlaying(Alarm))
	//	PlaySound(Alarm);	
}
void VIN_SoundManager::PlayPickup()
{
	PlaySound(Pickup);
}
void VIN_SoundManager::PlayKnifeSlash()
{

}
void VIN_SoundManager::PlayStart()
{
	PlaySound(Start);
}
void VIN_SoundManager::PlayPlayerDeath()
{

}

void VIN_SoundManager::PlayCityAmbient()
{

}
void VIN_SoundManager::PlayFarmAmbient()
{

}
void VIN_SoundManager::PlayDockAmbient()
{

}
void VIN_SoundManager::PlayFactoryAmbient()
{

}

void VIN_SoundManager::StopAllSounds()
{
	// Music
	if(CityID != -1 && fmod->IsSoundPlaying(CityID))
		fmod->StopSound(CityID);

	if(CityID != -1 && fmod->IsSoundPlaying(gameover))
		fmod->StopSound(gameover);

	if(TitleBGM != -1 && fmod->IsSoundPlaying(TitleBGM))
		fmod->StopSound(TitleBGM);

	if(MenuBGM != -1 && fmod->IsSoundPlaying(MenuBGM))
		fmod->StopSound(MenuBGM);

	if(CreditBGM != -1 && fmod->IsSoundPlaying(CreditBGM))
		fmod->StopSound(CreditBGM);

	if(Highscores != -1 && fmod->IsSoundPlaying(Highscores))
		fmod->StopSound(Highscores);

	// Sounds
	if(Explosion != -1 && fmod->IsSoundPlaying(Explosion))
		fmod->StopSound(Explosion);

	if(Woosh1 != -1 && fmod->IsSoundPlaying(Woosh1))
		fmod->StopSound(Woosh1);

	if(Woosh2 != -1 && fmod->IsSoundPlaying(Woosh2))
		fmod->StopSound(Woosh2);

	if(Woosh3 != -1 && fmod->IsSoundPlaying(Woosh3))
		fmod->StopSound(Woosh3);

	if(Punch1 != -1 && fmod->IsSoundPlaying(Punch1))
		fmod->StopSound(Punch1);

	if(Punch2 != -1 && fmod->IsSoundPlaying(Punch2))
		fmod->StopSound(Punch2);

	if(Punch3 != -1 && fmod->IsSoundPlaying(Punch3))
		fmod->StopSound(Punch3);

	if(Death1 != -1 && fmod->IsSoundPlaying(Death1))
		fmod->StopSound(Death1);

	if(Death2 != -1 && fmod->IsSoundPlaying(Death2))
		fmod->StopSound(Death2);

	if(Death2 != -1 && fmod->IsSoundPlaying(Alarm))
		fmod->StopSound(Alarm);


}

void VIN_SoundManager::StopGameSounds()
{
	if(Explosion != -1 && fmod->IsSoundPlaying(Explosion))
		fmod->StopSound(Explosion);

	if(Start != -1 && fmod->IsSoundPlaying(Start))
		fmod->StopSound(Start);

	if(Woosh1 != -1 && fmod->IsSoundPlaying(Woosh1))
		fmod->StopSound(Woosh1);

	if(Woosh2 != -1 && fmod->IsSoundPlaying(Woosh2))
		fmod->StopSound(Woosh2);

	if(Woosh3 != -1 && fmod->IsSoundPlaying(Woosh3))
		fmod->StopSound(Woosh3);

	if(Punch1 != -1 && fmod->IsSoundPlaying(Punch1))
		fmod->StopSound(Punch1);

	if(Punch2 != -1 && fmod->IsSoundPlaying(Punch2))
		fmod->StopSound(Punch2);

	if(Punch3 != -1 && fmod->IsSoundPlaying(Punch3))
		fmod->StopSound(Punch3);

	if(Death1 != -1 && fmod->IsSoundPlaying(Death1))
		fmod->StopSound(Death1);

	if(Death2 != -1 && fmod->IsSoundPlaying(Death2))
		fmod->StopSound(Death2);

	if(Death2 != -1 && fmod->IsSoundPlaying(Alarm))
		fmod->StopSound(Alarm);
}

void VIN_SoundManager::StopSound(int SoundID)
{
	if( SoundID != -1)
	{
		//if(SoundID < fmod->m_SoundList.size())
			fmod->StopSound(SoundID);
	}
}

void VIN_SoundManager::ResetAllSounds()
{

}
void VIN_SoundManager::ResetSound(int SoundID)
{

	if( SoundID != -1)
	{
		fmod->StopSound(SoundID);
		fmod->ResetSound(SoundID);
	}
}

void VIN_SoundManager::SetBGMVolume(float volume)
{
	fmod->SetVolume(CityID, volume);
	fmod->SetVolume(TitleBGM, volume);
	fmod->SetVolume(CreditBGM, volume);
	//fmod->SetVolume(MenuBGM, volume);
	fmod->SetVolume(Highscores, volume);
}

void VIN_SoundManager::SetSFXVolume(float volume)
{
	fmod->SetVolume(Punch1, volume);
	fmod->SetVolume(Punch2, volume);
	fmod->SetVolume(Punch3, volume);
	fmod->SetVolume(Punch4, volume);
	fmod->SetVolume(Level1, volume);
	fmod->SetVolume(Level2, volume);
	fmod->SetVolume(Hurt1, volume);
	fmod->SetVolume(Hurt2, volume);
	fmod->SetVolume(Hurt3, volume);
	fmod->SetVolume(Hurt4, volume);
	//fmod->SetVolume(Death1, volume);
	//fmod->SetVolume(Death2, volume);
	fmod->SetVolume(Explosion, volume);
	fmod->SetVolume(MenuConfirm, volume);
	fmod->SetVolume(MenuSelect, volume);
	fmod->SetVolume(Woosh1, volume);
	fmod->SetVolume(Woosh2, volume);
	fmod->SetVolume(Woosh3, volume);
	fmod->SetVolume(placerock, volume);
	fmod->SetVolume(Gunshot1, volume);
	fmod->SetVolume(Gunshot2, volume);
	fmod->SetVolume(Start, volume);
	fmod->SetVolume(Pickup, volume);

 }